package com.zd.wechatPlatform.controller.api;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zd.wechatPlatform.bean.model.WxAccount;
import com.zd.wechatPlatform.cache.map.WxAccountCache;
import com.zd.wechatPlatform.common.utils.EncryptUtil;


/**
 * 微信账号相关的API
 * @author lishi
 * @date 2017-12-19 下午9:11:55
 */
@Controller
public class WXAccountApi {
	
	@Autowired
	private WxAccountCache wxAccountCache;
	
	@ResponseBody
	@RequestMapping("/jssdk_signture")
	public Map<String, Object> jssdk_signature(String url, String accountId) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		WxAccount wxAccount = wxAccountCache.get(accountId);
		if (wxAccount == null) {
			resultMap.put("result", false);
			resultMap.put("msg", "找不到相应的微信公众号");
		} else {
			String noncestr = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 10);
			String timestamp = System.currentTimeMillis() + "";
			String jsapi_ticket = wxAccount.getJsapiTicket();

			resultMap.put("nonceStr", noncestr);
			resultMap.put("timestamp", timestamp);
			resultMap.put("appId", wxAccount.getAppId());

			StringBuffer sb = new StringBuffer();
			sb.append("jsapi_ticket=" + jsapi_ticket);
			sb.append("&noncestr=" + noncestr);
			sb.append("&timestamp=" + timestamp);
			sb.append("&url=" + url);
			String signature = EncryptUtil.Encrypt(sb.toString(), "SHA-1");
			resultMap.put("result", true);
			resultMap.put("signature", signature);
		}
		return resultMap;
	}
}
